package com.junchao.peng.service.impl;

import com.junchao.peng.mapper.MeetRoomMapper;
import com.junchao.peng.pojo.MeetRoom;
import com.junchao.peng.service.MeetRoomService;
import com.junchao.peng.utils.JsonResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author dell
 */
@Service("MeetRoomService")
public class MeetRoomServiceImpl implements MeetRoomService {

    final MeetRoomMapper meetRoomMapper;
    final RedisTemplate redisTemplate;

    public MeetRoomServiceImpl(MeetRoomMapper meetRoomMapper, RedisTemplate redisTemplate) {
        this.meetRoomMapper = meetRoomMapper;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public JsonResult deleteByPrimaryKey(Integer id, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        int i = meetRoomMapper.deleteByPrimaryKey(id);
        if (i != 0){
            return new JsonResult("删除成功");
        }
        return new JsonResult("删除失败请联系管理员",500);
    }

    @Override
    public JsonResult insert(MeetRoom record, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        int i = meetRoomMapper.insert(record);
        if (i != 0){
            return new JsonResult("添加成功");
        }
        return new JsonResult("添加失败请联系管理员",500);
    }

    @Override
    public JsonResult insertSelective(MeetRoom record, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        int i = meetRoomMapper.insertSelective(record);
        if (i != 0){
            return new JsonResult("添加成功");
        }
        return new JsonResult("添加失败请联系管理员",500);
    }

    @Override
    public JsonResult selectByPrimaryKey(Integer id, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        return new JsonResult(meetRoomMapper.selectByPrimaryKey(id));
    }

    @Override
    public JsonResult listMeetRoom(String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        return new JsonResult(meetRoomMapper.listMeetRoom());
    }

    @Override
    public JsonResult updateByPrimaryKeySelective(MeetRoom record, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        int i = meetRoomMapper.updateByPrimaryKeySelective(record);
        if (i != 0){
            return new JsonResult("修改成功");
        }
        return new JsonResult("修改失败请联系管理员",500);
    }

    @Override
    public JsonResult updateByPrimaryKey(MeetRoom record, String token) {
        if (getToken(token)){
            return new JsonResult("用户未登录",403);
        }
        int i = meetRoomMapper.updateByPrimaryKey(record);
        if (i != 0){
            return new JsonResult("修改成功");
        }
        return new JsonResult("修改失败请联系管理员",500);
    }

    private boolean getToken(String token){
        boolean flag = false;
        Object redisCode = redisTemplate.opsForValue().get(token);
        if (redisCode != null){
            flag = true;
        }
        return !flag;
    }
}
